


var MOD_EST= {
  oblaw:"EPC",
  oblan:"EN",
  currentclause:"",
  currentlabel:"New",
  newmodified:0,
  newlabel:"Give me a label",
  stdcls_cl1:1,
  stdcls_cl2:"",
  stdcls_expr1:"@#$!%%",
  stdcls_expr2:"",
  currenttype:"",
  alllangs:0,
  alllaws:0
};


MOD_EST.onchangestdcl=function() {
  var txt=document.getElementById("instd").value; 
  var odict= new objectiondict();

  var clist= new List_iterator(document.getElementById("stdcls1").value);
  while(!clist.end()) {
    odict.add(clist.getValue(),document.getElementById("stdexpr1").value);
    clist.next();
  }
  
  var clist= new List_iterator(document.getElementById("stdcls2").value);
  while(!clist.end()) {
    odict.add(clist.getValue(),document.getElementById("stdexpr2").value);
    clist.next();
  }
  
  var ctxt= new stdcls_ctxt();
  ctxt.add("clstype",MOD_EST.currentclause.get_attr("type"));

  ctxt.add("odict",odict);
  ctxt.add("lang",MOD_EST.oblan);
  ctxt.add("law",MOD_EST.oblaw);

  ctxt.add("claims",odict.claims());
  ctxt.add("exprs" ,odict.exprs());
 
  html_flexarea_settxt("outstd",stdcls_expr(ctxt,txt));
 
}


MOD_EST.clickondbg=function() {
  if(MOD_STC.dbg) {
    MOD_STC.dbg=0;
  } else {
    MOD_STC.dbg=1;
  }

}

MOD_EST.onlcksaveobj=function(){
  SaveStdClauses();
  alert("Changes saved");
}

MOD_EST.onckactive=function(){
  if(MOD_EST.currentclause.get_attr("active")) {
    MOD_EST.currentclause.set_attr("active","");
  } else {
    MOD_EST.currentclause.set_attr("active","yes");
  }
  rebuild_clars();
  rebuild_hints();
}


MOD_EST.onchoblang=function(){
  MOD_EST.oblan=html_selected_iter("oblan");
  edit_stdcls();

  //  
  //html_flexarea_settxt("instd",MOD_EST.currentclause._getText(MOD_EST.oblan,MOD_EST.oblaw));
}


MOD_EST.onchoblaw=function(){
  MOD_EST.oblaw=html_selected_iter("oblaw",new lang_iterator());
  edit_stdcls();
  //
  //html_flexarea_settxt("instd",MOD_EST.currentclause._getText(MOD_EST.oblan,MOD_EST.oblaw));
}

MOD_EST.onchlabel=function(label) {
  if(label) {
    if(MOD_EST.currentlabel=="New"){
      //alert("Create "+currentlabel);
      CreateStdClause(MOD_EST.currenttype,label); //currentclause.copy(label);
    } else {
      MOD_EST.currentclause.set_attr("label",label);          
    }
    MOD_EST.currentlabel=label;
  } else {
    alert("Bad label");
  }
  edit_stdcls();
}

MOD_EST.onchdesc=function(name){
  MOD_EST.currentclause.set_attr("desc",name);
}

MOD_EST.onchtype=function(name){
  MOD_EST.currentclause.set_attr("type",name);
}


MOD_EST.onchstditer=function(){
  MOD_EST.currentlabel=html_selected("stdsiter");
  //alert("Redraw "+MOD_EST.currentlabel);
  edit_stdcls();
}

MOD_EST.onlckcancelobj=function() {
  MOD_EST.currentlabel="New";
  ReloadStdClauses();
  edit_stdcls();
}

MOD_EST.onchstdtext=function() {
  var newtxt=document.getElementById('instd').value;
  if(MOD_EST.currentlabel=="New") {
    alert("Give a new label first !");
  } else {
    MOD_EST.currentclause.setText(newtxt,MOD_EST.oblan,MOD_EST.oblaw);
  }
}

MOD_EST.onckshowC=function() {
  if(MOD_EST.currentclause.get_attr("showC")=="yes") {
    MOD_EST.currentclause.set_attr("showC","no");
  } else {
    MOD_EST.currentclause.set_attr("showC","yes");
  }
  rebuild_clars()
}

MOD_EST.onckshowGC=function() {
  if(MOD_EST.currentclause.get_attr("showGC")=="yes") {
    MOD_EST.currentclause.set_attr("showGC","no");
  } else {
    MOD_EST.currentclause.set_attr("showGC","yes");
  }
  rebuild_clars()
}

MOD_EST.onckshow=function() {
  if(MOD_EST.currentclause.get_attr("showH")=="yes") {
    MOD_EST.currentclause.set_attr("showH","no");
  } else {
    MOD_EST.currentclause.set_attr("showH","yes");
  }
  rebuild_hints();
}

MOD_EST.onckuniv=function() {
  if(MOD_EST.currentclause.get_attr("univ")!="no") {
    MOD_EST.currentclause.set_attr("univ","no");
  } else {
    MOD_EST.currentclause.set_attr("univ","yes");
  }
}

MOD_EST.onlckdelobj=function(){
  if(confirm("Delete "+MOD_EST.currentclause.label()+" ?" )) {
    MOD_EST.currentlabel="";
    MOD_EST.currentclause.rm();
    rebuild_hints();
    rebuild_clars();
    edit_stdcls()
  }
}


MOD_EST.stdalllang=function() {
  if(MOD_EST.alllangs) {
    MOD_EST.alllangs=0;
  } else {
    MOD_EST.alllaws=0;
    MOD_EST.alllangs=1;
  }
  edit_stdcls();
}

MOD_EST.stdalllaw=function() {
  if(MOD_EST.alllaws) {
    MOD_EST.alllaws=0;
  } else {
    MOD_EST.alllangs=0;
    MOD_EST.alllaws=1;
  }
  edit_stdcls();
}

MOD_EST.stdtxtarea=function(text,label,opts) {
  var html="<TR><TD colspan=2>";
  if(label) {
    html+="<fieldset><legend>"+label+"</legend>";
  }
  html+=html_flexarea(text,"MOD_EST.onchstdtext()",(opts && "id='instd'") || "readonly",80,4);    
  if(label) {
    html+="</fieldset>";
  }
  html+="</TD>";
  if(opts) {
    html+="<TD align=right>";
    html+="<button onclick='MOD_EST.onlcksaveobj();'>"+html_img(ok_img)+"</button><BR>";
    html+="<button onclick='MOD_EST.onlckcancelobj();'>"+ html_img(cancel_img)+"</button><BR>"; 
    html+="<button onclick='MOD_EST.onlckdelobj();'>"+ html_img(trash_img)+"</button><BR>";
    html+="</TD>";
  }
  html+="</TR>";
  return html;
}



MOD_EST._edit_stdcls=function(){
  var currentdesc="",currenttxt="",newro;
  MOD_EST.currenttype= html_tabs_obj_selected("stdview");

  if(MOD_EST.currentlabel!="New") {      
    //alert("Find "+currentlabel+" of "+stdview);
    MOD_EST.currentclause=new StdClause_Iterator("@label=\""+MOD_EST.currentlabel+
						 "\" && @type=\""+MOD_EST.currenttype+"\"");      
    if(MOD_EST.currentclause.end()) {
      MOD_EST.currentlabel="New";
    }
  }    
  
  var std_iter= new StdClause_Iterator("@type=\""+MOD_EST.currenttype+"\" && @label!=\"New\"");
  
  //html="<TABLE width=100%><TR><TD>";
  //'style=border-collapse:collapse;'
  var html="<table width=100%  border=1><COL align=left><COL><COL align=left><COL align=left>";
  
  html+="<TR>";
  if(MOD_EST.currentlabel=="New") {
    html+="<TH colspan=2 > Clause ";
  } else {
    html+="<TH> Clause ";
  }
  html+=html_selection_iter("stdsiter",
			    std_iter,
			    MOD_EST.currentlabel,
			    "MOD_EST.onchstditer();","","New")+"</TH>";
  
  html+="<TD align=right>";  
  if(MOD_EST.currentlabel!= "New") {
    var dis=(MOD_EST.currentlabel=="header") ? " disabled ": ""; 
    currentdesc=MOD_EST.currentclause.desc(); 
    MOD_EST.currenttype=MOD_EST.currentclause.type();
    currenttxt=MOD_EST.currentclause._getText(MOD_EST.oblan,MOD_EST.oblaw);     
    switch(MOD_EST.currenttype) {
    case "clarity":
      html+=" Show in C <input type=checkbox "+dis;	
      html+=(MOD_EST.currentclause.get_attr("showC")=="yes" ? "checked":"")+
	" onclick='MOD_EST.onckshowC()'>";

      html+=" Show in GC <input type=checkbox "+dis;	
      html+=(MOD_EST.currentclause.get_attr("showGC")=="yes" ? "checked":"")+
	" onclick='MOD_EST.onckshowGC()'>";

      break;
    case "inventive":
      html+=" Show in H <input type=checkbox "+dis;	
      html+=(MOD_EST.currentclause.get_attr("showH")=="yes" ? "checked":"")+
	" onclick='MOD_EST.onckshowH()'>";
      break;
    default:
    }
    html+=" Universal <input type=checkbox "+dis;
    html+=(MOD_EST.currentclause.get_attr("univ")!="no" ? "checked":"")+
      " onclick='MOD_EST.onckuniv()'>";
    
    html+=" Active <input type=checkbox ";  
    html+=(MOD_EST.currentclause.get_attr("active") ? "checked":"")+
      " onclick='MOD_EST.onckactive()'>";
    newro="";
  } else { // entry for new 
    newro=" disabled ";
  }
  html+="</TD></TR><TR>";
  html+="<TH colspan=2><fieldset style='width:10%'><legend>Label</legend><input type=text value='";
  
  if(MOD_EST.currentlabel=="New") {
      html+=MOD_EST.newlabel;
    } else {
    html+=MOD_EST.currentlabel;
  }
  html+="' id='stdlabel' onchange='MOD_EST.onchlabel(this.value)'></fieldset>";
  
  html+="<fieldset style='width:10%'><legend>Type</legend> ";
  html+="<input type=text readonly size=8 onchange='MOD_EST.onchtype(this.value)' value='"+
  MOD_EST.currenttype+"' id='stdtype' ></fieldset>";
  
  html+=" <fieldset style='width=30%'><legend>Description</legend> ";
  html+="<input type=text size=60 "+newro+" onchange='MOD_EST.onchdesc(this.value)' value='"+
  currentdesc+"' id='stdname' ></fieldset>";
  
  var langimg=minicompactview_img;
  if(MOD_EST.alllangs) {
    langimg=minifullview_img;
  }
  html+="<fieldset style='width:9%'><legend>Lang</legend>"+
  html_selection_iter("oblan",new lang_iterator(),MOD_EST.oblan,"MOD_EST.onchoblang()",newro)+
  html_img(langimg,"alllang","onclick='MOD_EST.stdalllang()'")+"</fieldset> "; 
      
  var lawimg=minicompactview_img;
  if(MOD_EST.alllaws) {
    lawimg=minifullview_img;
  }

  html+="<fieldset style='width:11%'><legend>Law </legend>"+
  html_selection_iter("oblaw",new law_iterator(),MOD_EST.oblaw,"MOD_EST.onchoblaw()",newro)+
  html_img(lawimg,"alllaw","onclick='MOD_EST.stdalllaw()'")+"</fieldset></TH>"; 
  html+="</TR>";

  if(MOD_EST.currentlabel!="New") {
    if(MOD_EST.alllangs) {
      var lang_iter= new lang_iterator();
      while(!lang_iter.end()) {
	html+=MOD_EST.stdtxtarea(MOD_EST.currentclause._getText(lang_iter.getValue(),MOD_EST.oblaw),
				 lang_iter.getValue(),lang_iter.getValue()==MOD_EST.oblan);  
	lang_iter.next();
      }
    } else if(MOD_EST.alllaws) {
      var law_iter= new law_iterator();
      while(!law_iter.end()) {
	html+=MOD_EST.stdtxtarea(MOD_EST.currentclause._getText(MOD_EST.oblan,law_iter.getValue()),
				 law_iter.getValue(),law_iter.getValue()==MOD_EST.oblaw);  
	law_iter.next();
      }
    } else {
      html+=MOD_EST.stdtxtarea(currenttxt,"",1);  
    }
  }
  
  html+="</TABLE>";
  
  if(MOD_EST.currentlabel!= "New") {
    if(html_tabs_obj_selected("stdview") == "clarity") {
      html+="<BR><h2>Preview <input type=button color=orange onclick='MOD_EST.onchangestdcl();'  value=Try></h2>";
      html+="<TABLE width=100%><TR>";
      html+="<th align=left> Expression <input type=text value='"+MOD_EST.stdcls_expr1+"' id='stdexpr1' "+newro+" >";
      html+=" in claim(s) <input type=text value='"+MOD_EST.stdcls_cl1+"' id='stdcls1' "+	newro+" ></th>";
      html+="</TR>";
      
      html+="<TR><th align=left> Expression <input type=text value='"+MOD_EST.stdcls_expr2+"' "+newro+"  id='stdexpr2'>";
      html+=" in claim(s) <input type=text value='"+MOD_EST.stdcls_cl2+"' "+newro+" id='stdcls2'></th>";
      html+="</TR>";

      //html+="<TR><TD colspan=4 align=center><button onclick='onchangestdcl()'>"+html_img(down_img)+"</button></TD></TR>";;
      html+="<TR><TD colspan=3>"+html_flexarea("","","id='outstd' readonly style='text-align:justify'",80,4)+"</TD>";
      html+="</TR></TABLE>";  
    }
  }
  return html;
}
 
MOD_EST._edit_allstdcls=function() {
  var iter_clause=new StdClause_Iterator();
  var html=""; 
  html="<TABLE border=1><TR><TH><input type=button class=FULL value=Type></TH><TH>Label</TH><TH>Description</TH><TH>Text</TH></TR>";
  var count=0;
  while(!iter_clause.end()) {
      html+="<TR  onclick='MOD_EST.currentlabel=\""+iter_clause.label()+"\"; ";
      html+=" html_tabs_doselect(\"stdview\",\""+iter_clause.type()+"\"); edit_stdcls();' ";
      html+="onmouseover='this.className=\"CLAUSEIN\";' ";
      if(count%2) {
	html+=" class='CLAUSEOUT1' onmouseout='this.className=\"CLAUSEOUT1\";'>";
      } else {
	html+=" class='CLAUSEOUT2' onmouseout='this.className=\"CLAUSEOUT2\";'>";
      }

      html+="<TD class=CLAUSESPAN>"+iter_clause.type()+"</TD>";
      html+="<TD class=CLAUSESPAN>"+iter_clause.label()+"</TD>";
      html+="<TD class=CLAUSESPAN>"+iter_clause.desc()+"</TD>";
      html+="<TD class=CLAUSESPAN>"+iter_clause.getText("EN").substring(0,50)+"...</TD></TR>";
      count++;

     iter_clause.next();
    }
    html+="</TABLE>";
    return html;
}
 
 

  //ClaimToolTitle();
  //CT_show_menu("Edit");
  //CT_show_menu("Comms");
  //CT_show_menu("Tools");

MOD_EST.tab=[];
MOD_EST.tab["All"]      ={ callback:MOD_EST._edit_allstdcls};
MOD_EST.tab["concise"]  ={ label:"Concise", callback:MOD_EST._edit_stdcls};
MOD_EST.tab["clarity"]  ={ label:"Clarity", callback:MOD_EST._edit_stdcls};
MOD_EST.tab["novelty"]  ={ label:"Novelty", callback:MOD_EST._edit_stdcls};
MOD_EST.tab["inventive"]={ label:"Inventive", callback:MOD_EST._edit_stdcls};
MOD_EST.tab["grant"]    ={ label:"Grant", callback:MOD_EST._edit_stdcls};
MOD_EST.tab["unity"]    ={ label:"Unity", callback:MOD_EST._edit_stdcls};
MOD_EST.tab["misc"]     ={ label:"Misc", callback:MOD_EST._edit_stdcls}


function edit_stdcls() {
  var title="<table width=100%><TR><TD><H3> Edit Standard Clauses</H3></TD><TD align=right> Debug ";
  title+="<input type=checkbox "+(MOD_STC.dbg ? "checked": "")+" onclick='MOD_EST.clickondbg();'></TD></TR></TABLE>";

 //html_display(html);
  html_display(title+html_tabs_obj("stdview",MOD_EST.tab,MOD_EST.currenttype));
}  
